<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-10-10
 * Time: 17:20
 */

namespace app\src\base\helper;


use app\src\session\logic\LoginSessionLogic;
use think\Session;

class SessionHelper
{


    const EXPIRE_LOGIN_TIME = 1296000;// 3600 * 24 * 15;

    /**
     * 检查登录状态
     * @param $uid   integer 用户id
     * @param $log_session_id string 用户登录后的授权id
     * @return array
     */
    public static function checkLoginSession($uid,$log_session_id){
        $log_session_id = empty($log_session_id) ? "-1":$log_session_id;
        $logic  = new LoginSessionLogic();
        $map    = ['uid'=>$uid];
        $result = $logic->getInfo($map);

        $now  = time();
        if(ValidateHelper::legalArrayResult($result)){
            $info = $result['info'];
            $expire_time = intval($info['expire_time']);

            if($now > $expire_time){
                return ['status'=>false,'info'=>lang("err_re_login")];
            }

            if($log_session_id != "-1" && $log_session_id != $info['log_session_id']){
                return ['status'=>false,'info'=>lang("err_login_".$info['login_device_type'])];
            }

            //检测成功,更新过期时间
            $result = $logic->save($map,['expire_time'=>$now + self::EXPIRE_LOGIN_TIME]);

            return $result;
        }else{

            $entity = [
                'update_time'       =>  $now,
                'uid'               =>  $uid,
                'login_info'        =>  get_client_ip(),
                'create_time'       =>  $now,
                'expire_time'       =>  0,
                'log_session_id'    =>  '',
                'login_device_type' =>  '',
            ];

            $result = $logic->add($entity);
            if(!$result['status']){
                return $result;
            }
        }

        return ['status'=>false,'info'=>lang("err_re_login")];
    }

    /**
     * 记录 Session 信息
     * @author hebidu <email:346551990@qq.com>
     * @param $uid
     * @param $device_token
     * @param $device_type
     * @return string
     */
    public static function addLoginSession($uid,$device_token,$device_type){
        $logic  = new LoginSessionLogic();
        $map    = ['uid'=>$uid];

        self::checkLoginSession($uid,"");

        $now = time();
        $log_session_id = md5($uid.$device_token.$device_type.time());
        $entity = [
            'log_session_id'=>$log_session_id,
            'expire_time'=> $now + self::EXPIRE_LOGIN_TIME,
            'update_time'=> $now,
            'login_device_type'=>$device_type
        ];

        $result = $logic->save($map,$entity);



        return $result;
    }



    public static function loginCheck($login_uid,$log_session_id,$device_type){
        $logic = new LoginSessionLogic();
        $map = ['uid'=>$login_uid];
        $result= $logic->getInfo($map);
        $login_info = get_client_ip();
        $now = time();

        if($result['status']){
            if(empty($result['info'])){
                //清除该uid对应的其它记录

                $entity = [
                    'session_id'=>$log_session_id,
                    'login_info'=>$login_info,
                    'device_type'=>$device_type,
                    'expire_time'=> $now + self::EXPIRE_LOGIN_TIME,
                    'update_time'=> $now,
                    'create_time'=> $now,
                    'uid'=>$login_uid
                ];

                return $logic->add($entity);
            }else{
                $info = $result['info'];

                //说明已经过期
                if($info['expire_time'] > time()){
                    return ['status'=>false,'info'=>lang("err_re_login")];
                }

                $entity = [
                    'session_id' => $log_session_id,
                    'login_info' => $login_info,
                    'device_type'=> $device_type,
                    'expire_time'=> $now + self::EXPIRE_LOGIN_TIME,
                    'update_time'=> $now
                ];

                return $logic->save($map,$entity);
            }
        }

        return $result;
    }
    
    public static function logout(){
        Session::clear();
    }

    public static function setUserInfo($userinfo){
         session(SessionKeys::USER,$userinfo);
    }

}